<template>
  <!-- 添加或修改博客对话框 -->
  <vxe-modal :title="title" v-model="open" width="60%" showFooter show-zoom resize>
    <el-form ref="formDialog" :model="form" :rules="rules" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类别" prop="category">
            <el-select v-model="form.category" placeholder="请选择类别">
              <el-option
                v-for="dict in dict.type.blog_cat"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="封面" prop="cover">
        <image-upload v-model="form.cover"/>
      </el-form-item>
      <el-form-item label="内容">
        <editor v-model="form.content" :height="300"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer  dialog-footer-center">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </vxe-modal>
</template>

<script>
import {addBlog, getBlog, updateBlog} from "@/api/huacai/blog";

export default {
  name: "AddOrUpdateBlog",
  dicts: ['blog_cat'],
  data() {
    return {
      open: false, //是否弹出弹窗
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        cover: [
          {required: true, message: "封面不能为空", trigger: "blur"}
        ],
        content: [
          {required: true, message: "内容不能为空", trigger: "blur"}
        ],
        category: [
          {required: true, message: "类别不能为空", trigger: "change"}
        ],
      },
    }
  },
  methods: {
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加博客";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const blogId = row.blogId
      getBlog(blogId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改博客";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          if (this.form.blogId != null) {
            updateBlog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok')
            });
          } else {
            addBlog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok')
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        blogId: null,
        title: null,
        cover: null,
        content: null,
        category: null,
        views: null,
        userId: null,
        createTime: null
      };
      this.resetForm("formDialog");
    },
  }
}
</script>

<style scoped>

</style>

<style scoped lang="scss">

</style>
